package leetcode;

import java.util.HashSet;
import java.util.Set;

public class LC25 {
    /*
        新解法：
            使用两个步长不同的指针，若链中有环则两个指针会相等，若无环快指针会指向null
     */
    public static void main(String[] args) {
        LC25 lc25 = new LC25();
        System.out.println(lc25.hasCycle(ListNode.ln(new int[]{-21, 10, 17, 8, 4, 26, 5, 35, 33, -7, -16, 27, -12, 6, 29, -12, 5, 9, 20, 14, 14, 2, 13, -24, 21, 23, -21, 5})));
    }
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        while(head != null){
            if (set.contains(head)) return true;
            set.add(head);
            head = head.next;
        }
        return false;
    }
}
